let app = getApp();
let that = null;
export default {
	data() {
		return {
			isLoad: false,
			chargeInfo: {},
			vipComboId: 0,
			appIsAudit: false,
			showConfirmNotice: false,
			professionName: '' // 从缓存中读取的专业名称
		};
	},
	onLoad(options) {
		that = this;
	},
	onShow() {
		that.appIsAudit = app.globalData.appIsAudit;
		if (that.appIsAudit) {
			uni.reLaunch({
				url: '/pages/index/index'
			});
		}
		that.getInfo();
	},
	methods: {
		/**
		 * 格式化积分数值，超过10000的显示为x.x万
		 * @param {Number} score 积分数值
		 * @return {String} 格式化后的积分字符串
		 */
		formatScore(score) {
			if (score >= 10000) {
				return Math.floor(score / 1000) / 10 + '万';
			}
			return score.toString();
		},
		getInfo() {
		app.globalData.server
			.postRequest('user/vip/chargeInfo', {})
			.then(function(res) {
				console.log(res);
				that.isLoad = true;
				that.chargeInfo = res.data;
				// 从缓存中读取当前专业名称
				that.loadProfessionNameFromCache();
			})
			.catch(function(err) {
				console.log(err);
			});
	},
	
	/**
	 * 从缓存中读取当前专业名称
	 */
	loadProfessionNameFromCache() {
		try {
			const currentProfession = app.globalData.config.storage.getCurrentProfessionData();
			if (currentProfession && currentProfession.name) {
				that.professionName = currentProfession.name;
			} else {
				// 如果缓存中没有，设置为空字符串
				that.professionName = '';
			}
		} catch (error) {
			console.error('从缓存读取专业名称失败:', error);
			// 如果读取缓存失败，设置为空字符串
			that.professionName = '';
		}
	},
	
	/**
	 * 从缓存中获取专业ID
	 * @return {Number} 专业ID，如果缓存中没有则返回0
	 */
	getProfessionIdFromCache() {
		try {
			const currentProfession = app.globalData.config.storage.getCurrentProfessionData();
			if (currentProfession && currentProfession.id) {
				return currentProfession.id;
			}
			return 0;
		} catch (error) {
			console.error('从缓存读取专业ID失败:', error);
			return 0;
		}
	},
		toCourseTap() {
			let url = '/pages/practice/course/course';
			uni.navigateTo({
				url: url
			});
		},
		onPriceClick(options) {
			let key = options.currentTarget.dataset.index;
			let item = that.chargeInfo.vipComboList[key];
			that.vipComboId = item.id;
		},

		/**
		 * 确认充值
		 * @param {Object} options
		 */
		onChargeCommit(options) {
			if (that.vipComboId === 0) {
				app.showToast('请选择充值套餐');
				return;
			}
			uni.showModal({
				title: '充值确认',
				cancelText: '我不同意',
				confirmText: '我同意',
				content: '我已阅读并同意充值协议',
				success: (res) => {
					if (res.confirm) {
						app.globalData.server
								.getRequest('order/create', {
									type: 1,
									vip_comb_id: that.vipComboId,
									profession_id: that.getProfessionIdFromCache()
								})
							.then(function(res) {
								let id = res.data.order_id;
								let url = '/pages/pay/pay?id=' + id;
								uni.navigateTo({
									url: url
								});

							})
							.catch(function(res) {
								app.showToast('创建订单失败');
							});
					}
				}
			});
		}
	}
};